<html>
<head><meta charset="utf-8"><title>ptr::write_bytes overconstrains callers? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html">ptr::write_bytes overconstrains callers?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208386196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208386196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208386196">(Aug 28 2020 at 19:34)</a>:</h4>
<p>I noticed that <a href="https://doc.rust-lang.org/nightly/std/ptr/fn.write_bytes.html"><code>ptr::write_bytes</code></a> currently says that the "Additionally, the caller must ensure that writing count * size_of::&lt;T&gt;() bytes to the given region of memory results in a valid value of T. Using a region of memory typed as a T that contains an invalid value of T is undefined behavior."</p>
<p>But we have <a href="https://doc.rust-lang.org/nightly/std/alloc/trait.GlobalAlloc.html#method.alloc_zeroed"><code>alloc_zeroed</code></a> on <code>GlobalAlloc</code> which is implicitly provided to callers, and does not include such a clause in its documentation.</p>
<p>One of these is wrong. I suspect we don't actually need to assert that 0s are a valid bit-pattern in write_bytes, though, as there's no typed access happening there AFAICT.</p>



<a name="208386317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208386317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208386317">(Aug 28 2020 at 19:36)</a>:</h4>
<p>oh, I guess the default impl calls it with <code>*mut u8</code> and then it's fine</p>



<a name="208386390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208386390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208386390">(Aug 28 2020 at 19:36)</a>:</h4>
<p>but I am interested in seeing if we can relax the constraint anyway, it doesn't seem like it should be necessary</p>



<a name="208386436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208386436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208386436">(Aug 28 2020 at 19:36)</a>:</h4>
<p>Not only that it is *mut u8.<br>
It is a raw pointer.<br>
Even if it was *mut T its fine as long as you don't dereference that as T</p>



<a name="208386522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208386522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208386522">(Aug 28 2020 at 19:37)</a>:</h4>
<p>The documentation implies otherwise, though, which is my point.</p>



<a name="208386584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208386584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208386584">(Aug 28 2020 at 19:38)</a>:</h4>
<p>(For write_bytes)</p>



<a name="208386620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208386620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208386620">(Aug 28 2020 at 19:38)</a>:</h4>
<p>Oh I forgot that <code>write_bytes</code> is also on a raw pointer.<br>
then you're right, I don't think this constraint is necessary</p>



<a name="208425871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208425871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208425871">(Aug 29 2020 at 09:38)</a>:</h4>
<p>I think the comment is meant to refer to what happens later when the data is read again... like, if you use this to overwrite a reference with zeros, it's UB the next time the reference is treated as such</p>



<a name="208425877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%3A%3Awrite_bytes%20overconstrains%20callers%3F/near/208425877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.3A.3Awrite_bytes.20overconstrains.20callers.3F.html#208425877">(Aug 29 2020 at 09:39)</a>:</h4>
<p>the write itself is not UB though, it's the later read/copy/... that is UB.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>